import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import pluginVue from "eslint-plugin-vue";
import { defineConfig } from "eslint/config";
import eslintConfigPrettier from "eslint-config-prettier";

// 合并环境全局变量
const mergedGlobals = {
  ...globals.browser,
  ...globals.node,
  ...globals.jest,
  ...globals.es2021,
  process: "readonly"
};

// 动态环境规则配置
const envRules = process.env.NODE_ENV === "production" 
  ? { "no-console": "error", "no-debugger": "error" } 
  : { "no-console": "off", "no-debugger": "off" };

export default defineConfig([
  // 基础 ESLint 推荐配置
  js.configs.recommended,

  // TypeScript 推荐配置
  ...tseslint.configs.recommended,

  // Vue3 基础配置
  ...pluginVue.configs["flat/essential"],

  // Prettier 集成配置
  eslintConfigPrettier,

  // 全局配置
  {
    files: ["**/*.{js,mjs,cjs,ts,vue}"],
    languageOptions: {
      globals: mergedGlobals,
      parser: tseslint.parser,
      parserOptions: {
        ecmaVersion: "latest",
        sourceType: "module",
        jsxPragma: "React",
        ecmaFeatures: {
          jsx: true
        }
      }
    }
  },

  // 添加忽略配置（替代 .eslintignore）
  {  // 修正后的忽略配置
    ignores: [
      "**/node_modules",
      "dist"
    ]
  },

  // Vue 文件特殊配置
  {
    files: ["**/*.vue"],
    languageOptions: {
      parser: pluginVue.parser
    }
  },

  

  // 自定义规则配置
  {
    rules: {
      // ESLint 基础规则
      "no-var": "error",
      "no-multiple-empty-lines": ["warn", { max: 1 }],
      "no-unexpected-multiline": "error",
      "no-useless-escape": "off",

      // TypeScript 规则
      "@typescript-eslint/ban-ts-comment": "off",
      "@typescript-eslint/no-unused-vars": "error",
      "@typescript-eslint/prefer-ts-expect-error": "error",
      "@typescript-eslint/no-explicit-any": "off",
      "@typescript-eslint/no-non-null-assertion": "off",
      "@typescript-eslint/no-namespace": "off",
      "@typescript-eslint/semi": "off",

      // Vue 规则
      "vue/multi-word-component-names": "off",
      "vue/no-mutating-props": "off",
      "vue/attribute-hyphenation": "off",
      ...envRules
    }
  }
]);
